export const storageLocal = () => {
  return {
    /**
     * 储存对应键名的 Storage 对象
     * @param {string} k - 键名
     * @param {*} v - 键值
     */
    setItem(k, v) {
      try {
        const serializedValue = JSON.stringify(v);
        localStorage.setItem(k, serializedValue);
      } catch (error) {
        console.error("Error setting item to localStorage", error);
      }
    },

    /**
     * 获取对应键名的 Storage 对象
     * @param {string} k - 键名
     * @returns {*} - 键值
     */
    getItem(k) {
      try {
        const serializedValue = localStorage.getItem(k);
        if (serializedValue === null) {
          return null;
        }
        return JSON.parse(serializedValue);
      } catch (error) {
        console.error("Error getting item from localStorage", error);
        return null;
      }
    },

    /**
     * 删除对应键名的 Storage 对象
     * @param {string} k - 键名
     */
    removeItem(k) {
      try {
        localStorage.removeItem(k);
      } catch (error) {
        console.error("Error removing item from localStorage", error);
      }
    },

    /**
     * 删除此域的所有 Storage 对象
     */
    clear() {
      try {
        localStorage.clear();
      } catch (error) {
        console.error("Error clearing localStorage", error);
      }
    },
  };
};
